package entity

import (
	"fmt"
	"strings"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//Nzjl 年终奖励导入
type Nzjl struct {
	ID       int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	DPNM     string  `gorm:"column:DPNM"` //部门
	EMNO     string  `gorm:"column:EMNO"` //工号
	EMNM     string  `gorm:"column:EMNM"` //姓名
	TZYY     string  `gorm:"column:TZYY"` //补记项目
	EMID     int     `gorm:"column:EMID"` //员工id
	DPID     int     `gorm:"column:DPID"` //部门id
	FFND     int     `gorm:"column:FFND"` //发放季度
	YJJXTZJE float64 `gorm:"column:YJJXTZJE"`
	YWJXTZJE float64 `gorm:"column:YWJXTZJE"`
}

//TableName
func (Nzjl) TableName() string {
	return "SH_JHCW_JXFF_NZJJLTZ"
}

// BeforeSave
func (u *Nzjl) BeforeSave(tx *gorm.DB) (err error) {
	jd := u.FFND*10 + 4
	err = can_import(tx, jd)
	if err != nil {
		return err
	}
	var emid, dpid *int
	var dpnm *string
	var emno, emnm string
	tx.Raw(`select "EMID","EMNM" from "SH_JHCW_JXFF_YGJX" where  "EMNO" = ?`, u.EMNO).Row().Scan(&emid, &emnm)
	if emid == nil {
		err = tx.Raw(`select "DPID","DPNM","EMID","EMNO","EMNM" from "V_USER_DEPT" where "EMNO"=?`, u.EMNO).Row().Scan(&dpid, &dpnm, &emid, &emno, &emnm)
		if err != nil || emid == nil {
			err = fmt.Errorf("员工号%s在类别绩效或绩效系统中不存在", u.EMNO)
			return
		}
	}
	emnm_sys := strings.TrimSpace(emnm)
	emnm_imp := strings.TrimSpace(u.EMNM)
	if !strings.EqualFold(emnm_sys, emnm_imp) && !strings.Contains(emnm_sys, emnm_imp) {
		err = fmt.Errorf("员工号%s对应的姓名是%s,而提供姓名是%s", u.EMNO, emnm, u.EMNM)
		return
	}
	tx.Raw(`select "DPNM","DPID"  from "SH_JHCW_JXFF_YGJX" where "EMNO" = ?`, u.EMNO).Row().Scan(&dpnm, &dpid)
	if dpid == nil {
		err = fmt.Errorf("部门%s在类别绩效中不存在,或者类别绩效没有导入", u.DPNM)
		return
	}
	u.DPID = *dpid
	u.DPNM = *dpnm
	u.EMID = *emid
	return
}

//ShCompClieQygmEF 返回结构体
func NzjlEF() base.EntityFunc {
	return func() interface{} {
		entity := Nzjl{}
		return &entity
	}
}

//ShCompClieQygmESF 返回结构体数组
func NzjlESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Nzjl
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("Nzjl", base.NewDefaultDao(NzjlEF(), NzjlESF(), nil, "my"))
}
